from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

import os, time

class F_QQMail():
    def __init__(self):
        self.f_name = ""
        self.f_size = 0
        self.f_expire = 0
        self.f_link = ""
        self.f_del = None

class QQMail():
    def __init__(self):
        self.qqmail = "https://mail.qq.com/"
        fp = webdriver.FirefoxProfile()
        fp.set_preference("browser.download.folderList", 2)
        fp.set_preference("browser.download.manager.showhenStarting", True)
        fp.set_preference("browser.download.dir", os.getcwd())
        fp.set_preference("browser.download.manager.showAlertOnComplete", False)
        fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream, text/csv, application/zip, application/x-gzip, image/jpeg, video/mp4")#下载文件类型
        
        self.driver = webdriver.Firefox(firefox_profile = fp)
        self.wait = WebDriverWait(self.driver, 10)
        self.driver.implicitly_wait(10) # seconds

    def login(self):
        self.driver.get(self.qqmail)
        try:
            element = self.wait.until(EC.presence_of_element_located((By.ID,"login_frame")))
            self.driver.switch_to.frame("login_frame")
        except TimeoutException as e:
            raise Exception("Login Fail")
        else:
            user = os.environ['user']
            passw = os.environ['passw']
            self.driver.find_element_by_id('u').send_keys(user)
            self.driver.find_element_by_id('p').send_keys(passw)
            self.driver.find_element_by_id('login_button').send_keys(Keys.ENTER)
            try:
                self.refresh()
            except TimeoutException as e:
                raise Exception("Login Fail")
        finally:
            pass
    
    def refresh(self):
        import time
        time.sleep(3)
        self.driver.refresh()
        self.driver.get(self.driver.current_url)
        self.wait.until(EC.presence_of_element_located((By.ID,"useralias")))
        time.sleep(0.4)
    
    def temp_space(self):
        self.driver.find_element_by_id("folder_ftn").click()
        self.wait.until(EC.presence_of_element_located((By.ID,"mainFrame")))
        self.driver.switch_to.frame("mainFrame")

    def list_dir(self):
        file_list = self.driver.find_elements_by_class_name("ft_l_listItemContent")
        f_list = []
        for f in file_list:
            fq = F_QQMail()
            complete = f.find_element_by_xpath("./div[@class='ft_l_listInfo ctrl_complete']")
            fq.f_size = complete.find_element_by_xpath("./span[@class='ft_l_listItem_info ft_j_flSize']").text
            fq.f_expire = complete.find_element_by_xpath("./span[@class='ft_l_listItem_info expire']").text

            detail = f.find_element_by_xpath("./div[@class='ft_l_listDetail']")
            detail_infos = detail.find_element_by_xpath("./div[@class='ft_l_listItem_infos']")
            fq.f_name = detail_infos.find_element_by_xpath("./div[@class='ft_l_listItem_filename']").text

            complete_action = detail_infos.find_element_by_xpath("./div[@class='ft_l_listItem_detail ft_l_listAction action']")
            complete_action_download = complete_action.find_element_by_xpath("./a[@class='ft_i_action download']")
            complete_action_delete = complete_action.find_element_by_xpath("./a[@class='ft_i_action delete']")
            fq.f_del = complete_action_delete
            fq.f_link = complete_action_download.get_attribute("href")
            f_list.append(fq)
        self.file = f_list
        return f_list

    def download(self, filename):
        for f in self.file:
            if f.f_name == filename:
                js = 'window.open("{}");'.format(f.f_link)
                self.driver.execute_script(js)
                break

    def delete(self, filename):
        for f in self.file:
            if f.f_name == filename:
                f.f_del.click()
                break
        self.driver.switch_to.default_content()
        delete_confirm = self.driver.find_element_by_id("QMconfirm_QMDialog_confirm")
        delete_confirm.click()
        self.wait.until(EC.presence_of_element_located((By.ID,"mainFrame")))
        self.driver.switch_to.frame("mainFrame")
        

if __name__ == "__main__":
    qq_mail = QQMail()
    qq_mail.login()
    qq_mail.temp_space()
    for i in qq_mail.list_dir():
        qq_mail.download(i.f_name)
        qq_mail.delete(i.f_name)
